import {Injectable} from '@angular/core';
import {AppConfig} from '../config';
import {DatePipe} from '@angular/common';
import {StopInfoStatistics} from '../entity/stopInfo-statistics.model';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class StopInfoStatisticsService {
  private fomat: string = 'yyyy-MM-dd';
  private readonly baseUrlPer = `${AppConfig.APP_RESTFUL}`;
  private readonly param = 'rStopInfoStatisticses';
  private readonly url = `${this.baseUrlPer}/${this.param}`;

  constructor(
    private http: InterceptorService,
    private datePipe: DatePipe) {
  }

  //获取公司级昨日损失电量
  getYesterDayPowerLoss(dateTime: Date): Promise<StopInfoStatistics[]> {
    let date = this.datePipe.transform(dateTime, 'yyyy-MM-dd');
    return this.http.get(`${this.url}/stopType/0/dateType/1/date/${date}`)
      .toPromise()
      .then(response => {
        let resault = response;
        try {
          switch (resault.code) {
            case '200':
              return resault.data as StopInfoStatistics[];
            case '-1':
              return [];
          }
        } catch (error) {
          throw error;
        }
      }).catch(this.handleError);
  }

  private handleError(error: any): Promise<any> {
    return Promise.reject(error.message || error);
  }

}
